package ${configuration.javaModelPackage}.service.impl;


import javax.annotation.Resource;

import org.springframework.stereotype.Service;


import java.util.List;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import tk.mybatis.mapper.entity.Example;
import ${configuration.javaModelPackage}.entity.${configuration.domainObjectName};
import ${configuration.javaModelPackage}.mapper.${configuration.domainObjectName}Mapper;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Calendar;

import lombok.extern.slf4j.Slf4j;

/**
 *
 * <p>Title: ${configuration.domainObjectName}ServiceImpl</p>
 * <p>Description: </p>
 * 
 * @author ${configuration.author}
 * @date ${.now?string("yyyy-MM")}
 */
@Slf4j
@Service
public class ${configuration.domainObjectName}ServiceImpl {
	
	@Resource
	private ${configuration.domainObjectName}Mapper mapper;
	@Resource
	private PageResultFactory pageResultFactory;

	@Transactional(propagation= Propagation.SUPPORTS)
	public PageResult gridQuery(${configuration.domainObjectName} obj, PageParam pageParam){
        //设置分页
        Page<${configuration.domainObjectName}> page = PageHelper.startPage(pageParam.getP(), pageParam.getC());
        Example example = new Example(${configuration.domainObjectName}.class);
        try {
        	//查询
        	List<${configuration.domainObjectName}> list =  this.mapper.selectByExample(example);
        	//取分页信息
        	int total = (int) page.getTotal();
        	//分页类

        	return pageResultFactory.createPageResult(pageParam.getP(), total, list );
        } catch (Exception e) {
			log.error("call ${configuration.domainObjectName} gridQuery occur an error!", e);
			throw SystemBaseExceptionEnum.COMMON_ERROR.getException("查询失败");
		}
	}

	@Transactional(propagation = Propagation.SUPPORTS)
	public List<${configuration.domainObjectName}> queryByExample(Example example) {
		if (null == example) {
			log.debug("${configuration.domainObjectName} queryByExample example was empty");
			return null;
		}

		try {
			return this.mapper.selectByExample(example);
    	} catch (Exception e) {
			log.error("call ${configuration.domainObjectName} queryByExample occur an error!", e);
			throw SystemBaseExceptionEnum.COMMON_ERROR.getException("根据通用条件查询失败");
    	}
	}

	@Transactional(propagation = Propagation.SUPPORTS)
	public ${configuration.domainObjectName} selectOneByExample(Example example) {
		if (null == example) {
			log.debug("${configuration.domainObjectName} selectOneByExample example was empty");
			return null;
		}

		try {
			return this.mapper.selectOneByExample(example);
		} catch (Exception e) {
			log.error("call ${configuration.domainObjectName} selectOneByExample occur an error!", e);
			throw SystemBaseExceptionEnum.COMMON_ERROR.getException("根据通用条件查询单条信息失败");
		}
	}

	@Transactional(propagation= Propagation.REQUIRED)
	public ${configuration.domainObjectName} save(${configuration.domainObjectName} pObj){
		String account = LoginUserUtil.getUserInfo().getUser().getAccount();
		pObj.setUpdateUser(account);
		try {
			//新增
			<#if configuration.pkTypeJava !="String">
			if (pObj.getId() == null) {
			<#else>
			if (StringUtils.isBlank(pObj.getId())) {
				pObj.setId(UUIDUtils.getUUID());
			</#if>
				pObj.setCreateUser(account);
				pObj.setCreateTime(Calendar.getInstance().getTime());
				this.mapper.insertSelective(pObj);
			} else {
				//修改
				this.mapper.updateByPrimaryKeySelective(pObj);
			}
		} catch (Exception e) {
			log.error("call ${configuration.domainObjectName} save occur an error!", e);
			throw SystemBaseExceptionEnum.COMMON_ERROR.getException("保存失败");
		}
		return pObj;
	}

	@Transactional(propagation= Propagation.REQUIRED)
	public void delete(List<String> ids){
		for (String id : ids) {
			try {
			<#if configuration.pkTypeJava !="String">
				this.mapper.delete(Long.parseLong(id));
			<#else>
			if (StringUtils.isNotBlank(id)) {
				this.mapper.deleteByPrimaryKey(id);
				}
			</#if>
				
			} catch (Exception e) {
				log.error("call delete ${configuration.domainObjectName} occur an error!", e);
				throw SystemBaseExceptionEnum.COMMON_ERROR.getException("删除失败");
			}
		}
	}

}
